package com.ruoyi.ftrl.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ftrl.domain.RoomInfo;
import com.ruoyi.ftrl.service.IRoomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 房间信息Controller
 *
 * @author dihang
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/ftrl/room")
public class RoomInfoController extends BaseController {
    @Autowired
    private IRoomInfoService roomInfoService;


    /**
     * 根据楼栋id获取房间集合
     */
    @GetMapping(value = "/room/{id}")
    public TableDataInfo getInfoList(@PathVariable("id") Long id) {
        List<RoomInfo> roomInfos = roomInfoService.selectRoomInfoByParentId(id);
        return getDataTable(roomInfos) ;
    }


    /**
     * 查询房间信息列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(RoomInfo roomInfo) {
        startPage();
        List<RoomInfo> list = roomInfoService.selectRoomInfoList(roomInfo);
        return getDataTable(list);
    }

    /**
     * 导出房间信息列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:room:export')")
    @Log(title = "房间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoomInfo roomInfo) {
        List<RoomInfo> list = roomInfoService.selectRoomInfoList(roomInfo);
        ExcelUtil<RoomInfo> util = new ExcelUtil<RoomInfo>(RoomInfo.class);
        util.exportExcel(response, list, "房间信息数据");
    }

    /**
     * 获取房间信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:room:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(roomInfoService.selectRoomInfoById(id));
    }

    /**
     * 新增房间信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:room:add')")
    @Log(title = "房间信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RoomInfo roomInfo) {
        return toAjax(roomInfoService.insertRoomInfo(roomInfo));
    }

    /**
     * 修改房间信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:room:edit')")
    @Log(title = "房间信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RoomInfo roomInfo) {
        return toAjax(roomInfoService.updateRoomInfo(roomInfo));
    }

    /**
     * 删除房间信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:room:remove')")
    @Log(title = "房间信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(roomInfoService.deleteRoomInfoByIds(ids));
    }

    /**
     * 识别房间户型（根据小区名称）
     * @param name
     * @return
     */
    @Anonymous
    @PutMapping("/room/name")
    public AjaxResult updateType(String name) {
       return toAjax(roomInfoService.updateRoomInfoState(name));
    }
}
